package com.yorick.authority.controller;

import com.yorick.authority.enums.Account;
import com.yorick.authority.modal.request.menu.MenuAddRequest;
import com.yorick.authority.modal.request.menu.MenuDetailRequest;
import com.yorick.authority.modal.request.menu.MenuEditRequest;
import com.yorick.authority.modal.request.menu.MenuListRequest;
import com.yorick.authority.service.MenuService;
import com.yorick.common.Result.SimpleFactory.ResultFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Map;

/**
 * @program: vr_manage
 * @description: 菜单管理
 * @author: Yorick
 * @create: 2022-05-16 19:35
 **/
@Slf4j
@Controller
@RequestMapping("menu")
public class MenuController {
    @Resource
    MenuService menuService;

    @RequestMapping(value = "menuList", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> menuList(@Valid @RequestBody MenuListRequest requestBody,
                                        HttpServletRequest request) {
        HttpSession session = request.getSession();
        requestBody.setAid(Integer.parseInt(session.getAttribute(Account.aid.name()).toString()));
        requestBody.setSuperRole(Boolean.parseBoolean(session.getAttribute(Account.superRole.name()).toString()));
        return ResultFactory.success(menuService.menuList(requestBody)).display();
    }


    @RequestMapping(value = "addMenu", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> addMenu(@Valid @RequestBody MenuAddRequest requestBody,
                                       HttpServletRequest request) {
        HttpSession session = request.getSession();
        requestBody.setAid(Integer.parseInt(session.getAttribute(Account.aid.name()).toString()));
        return menuService.addMenu(requestBody);
    }

    @RequestMapping(value = "editMenu", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> editMenu(@Valid @RequestBody MenuEditRequest requestBody,
                                        HttpServletRequest request) {
        HttpSession session = request.getSession();
        requestBody.setAid(Integer.parseInt(session.getAttribute(Account.aid.name()).toString()));
        return menuService.editMenu(requestBody);

    }

    @RequestMapping(value = "menuDetail", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> menuDetail(@Valid @RequestBody MenuDetailRequest requestBody,
                                          HttpServletRequest request) {
        HttpSession session = request.getSession();
        requestBody.setSuperRole(Boolean.parseBoolean(session.getAttribute(Account.superRole.name()).toString()));
        return ResultFactory.success(menuService.menuDetail(requestBody)).display();
    }
}
